package com.example.orderserver.service;

import com.example.orderserver.entity.OrderInfo;
import com.example.orderserver.manager.OrderManager;
import com.example.orderserver.params.OrderParams;
import com.example.orderserver.params.PageOrderParams;
import com.example.orderserver.vo.OrderVO;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderManager orderManager;
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderService.class);
    public PageInfo<OrderVO> pageOrder(PageOrderParams pageOrderParams){
        if(pageOrderParams.getType() == 1){
            pageOrderParams.setSellId(null);
        } else{
            pageOrderParams.setBuyId(null);
        }
        PageHelper.startPage(pageOrderParams);
        List<OrderVO> list = orderManager.listOrder(pageOrderParams);
        PageInfo<OrderVO> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public OrderVO getOrder(String id) {
        return orderManager.getOrderById(id);
    }

    public void deleteOrder(String id) {
        orderManager.deleteOrder(id);
    }

    public void updateOrder(OrderParams orderParams) {
        OrderInfo orderInfo = orderManager.getOrder(orderParams.getId());
        if(orderParams.getMethod() == 0){
            orderParams.setMethod(orderInfo.getMethod());
        }
        if(orderParams.getStatus() == 0){
            orderParams.setStatus(orderInfo.getStatus());
        }
        BeanCopyUtils.copyPropertiesIgnoreNull(orderParams, orderInfo);
        orderManager.updateOrder(orderInfo);
    }

    public Result insertOrder(OrderParams orderParams) {
        OrderInfo order = new OrderInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(orderParams, order);
        orderManager.insertOrder(order);
        return new Result(order.getId(), ReturnEnum.SUCCESS);
    }

}
